
$(document).ready(function() {
	getMenu();
	$(".menu").sortable({
		placeholder : "ui-state-default",
		items : ".bs-example",
	});
	
});

function manageMenu(manageType, menuId, parentId, previousId, menuName,
		pageName, linkName) {
	var maxNumber = 0;
	if (menuName === undefined) {
		menuName = "";
	}
	if (pageName === undefined) {
		pageName = "";
	}
	if (linkName === undefined) {
		linkName = "";
	}
	if (previousId === undefined) {

		$(".bs-example").each(function() {

			var id = parseInt($(this).prop("id"));
			if (id > maxNumber) {
				maxNumber = id;
			}

		});
		maxNumber++;

	} else {
		maxNumber = parseInt(menuId);

		if (previousId == "" || previousId == null) {

			previousId = "0";
		}

		menuId = previousId;

	}
	var minusClass = "";
	if (parentId == "0") {
		minusClass = "minus1";
	} else {
		minusClass = "minus2";
	}

	if (manageType == "plus") {

		var html = "";
		html += "<div class=\"row bs-example sort_" + maxNumber + "\" id=\""
				+ maxNumber + "\">";
		html += "<div class=\"row disabled\" id=\"menu_" + maxNumber + "\">";
		html += "<div class=\"col-sm-3\">";
		html += "<label>ชื่อเมนู</label> <input class=\"form-control\" type=\"text\" id=\"menu_"
				+ maxNumber
				+ "_menu_name\" value=\""
				+ menuName
				+ "\" placeholder=\"ชื่อเมนู\"/>";
		html += "</div>";
		html += "<div class=\"col-sm-3\">";
		html += "<label>รหัสหน้า</label> <input class=\"form-control\" type=\"text\" id=\"menu_"
				+ maxNumber
				+ "_menu_id\" value=\""
				+ pageName
				+ "\" placeholder=\"ชื่อหน้า\"/>";
		html += "</div>";
		html += "<div class=\"col-sm-3\">";
		html += "<label>ลิงก์</label> <input class=\"form-control\" type=\"text\" id=\"menu_"
				+ maxNumber
				+ "_menu_url\" value=\""
				+ linkName
				+ "\" placeholder=\"ลิงก์\"/>";
		html += "</div>";
		html += "<div class=\"col-sm-3\">";
		html += "<br/>";
		html += "<button type=\"button\" class=\"btn btn-primary\" title=\"เพิ่มเมนู\" onclick=\"manageMenu('plus','"
				+ maxNumber
				+ "')\"><i class=\"glyphicon glyphicon-plus\"></i></button>&nbsp;";
		html += "<button type=\"button\" class=\"btn btn-info\" title=\"เพิ่มเมนูย่อย\" onclick=\"manageMenu('plus','menu_"
				+ maxNumber
				+ "')\"><i class=\"glyphicon glyphicon-plus\"></i></button>&nbsp;";
		html += "<button type=\"button\" class=\""
				+ minusClass
				+ " btn btn-danger\" title=\"ลบเมนู\" onclick=\"manageMenu('minus','"
				+ maxNumber
				+ "')\"><i class=\"glyphicon glyphicon-minus\"></i></button>";
		html += "</div>";
		html += "</div>";
		html += "</div>";

		var children = $("#" + menuId).children();

		if (parseInt(children.length) > 0) {
			$(html).insertAfter("#" + menuId);

		} else {
			$("#" + menuId).html(html);
		}

	} else {

		$("#" + menuId).remove();

	}

	var minus2Count = 0;
	$(".minus2").each(function() {
		minus2Count++;

	});
	if (minus2Count > 0) {

		$(".minus2").prop("disabled", false);
	} else {
		$(".minus2").prop("disabled", true);
	}

	$(".menu").children().children().children().eq(3).children().eq(3)
			.removeClass("minus2");
	$(".menu").children().children().children().eq(3).children().eq(3)
			.addClass("minus1");

	var children = $(".menu").children();
	if (parseInt(children.length) > 1) {

		$(".minus1").prop("disabled", false);
	} else {
		$(".minus1").prop("disabled", true);
	}
	
	$(".bs-example").each(function() {
		$("#" + $(this).prop("id")).sortable({
			placeholder : "ui-state-default",
			items : ".sort_" + $(this).prop("id"),
		});
		$("#" + $(this).prop("id")).disableSelection();
	});

}

function saveData() {
	var id;

	var menu_name = [];
	var menu_id = [];
	var menu_url = [];
	var menu_route_parent_id = [];
	var menu_route_previous_id = [];
	var menu_parent = [];
	var menu_parent_id = [];
	var menu_route_id = [];
	var i = 0;
	$(".bs-example")
			.each(
					function() {

						id = $(this).prop("id");
						menu_route_id[i] = id;
						menu_route_parent_id[i] = $(this).parent().prop("id");
						menu_name[i] = $("#menu_" + id + "_menu_name").val();
						menu_id[i] = $("#menu_" + id + "_menu_id").val();
						if ($(
								"#menu_" + $(this).parent().prop("id")
										+ "_menu_id").val() === undefined) {
							menu_parent_id[i] = menu_id[i];
						} else {
							menu_parent_id[i] = $(
									"#menu_" + $(this).parent().prop("id")
											+ "_menu_id").val();
						}

						menu_url[i] = $("#menu_" + id + "_menu_url").val();
						if ($(this).prev().prop("id") === undefined) {
							menu_route_previous_id[i] = "";
						} else {
							menu_route_previous_id[i] = $(this).prev().prop(
									"id");
						}

						var children = $(this).children();
						if (parseInt(children.length) > 1) {
							menu_parent[i] = "Y";
						} else {
							menu_parent[i] = "N";
						}
						i++;

					});

	$.ajax({
		url : "MenuMasterSrvl?process_type=saveData",
		type : "POST",
		data : {

			menu_id : menu_id,
			menu_name : menu_name,
			menu_url : menu_url,
			menu_parent_id : menu_parent_id,
			menu_parent : menu_parent,
			menu_route_previous_id : menu_route_previous_id,
			menu_route_id : menu_route_id,
			menu_route_parent_id : menu_route_parent_id,

		},
		async : false,
		dataType : 'json',
		success : function(result) {

			if (result.paMessage == "success") {
				$("#modal_content_message").html("บันทึกข้อมูลสำเร็จ");
			} else {
				$("#modal_content_message").html("บันทึกข้อมูลไม่สำเร็จ");
			}

			$('#messageModal').modal();
		}
	});
}

function getMenu() {
	$.ajax({
		url : "MenuMasterSrvl?process_type=getMenu",
		type : "POST",
		data : {},
		async : false,
		dataType : 'json',
		success : function(result) {
			if (result.voMenuRouteId.length == 0) {
				manageMenu('plus', '0', '0');
			} else {

				for (var i = 0; i < result.voMenuRouteId.length; i++) {
					manageMenu('plus', result.voMenuRouteId[i],
							result.voMenuRouteParentId[i],
							result.voMenuRoutePreviousId[i],
							result.voMenuName[i], result.voMenuId[i],
							result.voMenuURL[i]);
				}
			}

		}
	});
}
